<?php

namespace app\merchant\controller\shop;

use app\merchant\controller\Base;
use app\merchant\model\store\Shop as ShopModel;
use app\merchant\model\store\shop\Clerk as ClerkModel;
use app\merchant\model\User as UserModel;

/**
 * 门店店员控制器
 * Class Clerk
 * @package app\store\controller\shop
 */
class Clerk extends Base
{
    /**
     * 店员列表
     * @param int $shop_id
     * @param string $search
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $param = $this->postData();
        // 店员列表
        $model = new ClerkModel;
        $list = $model->getList($param);
        // 门店列表
        $shopList = ShopModel::getAllList();
        return $this->renderSuccess(compact('list', 'shopList'),"查询成功");
    }




    /**
     * 门店列表
     * @return mixed
     * @param string $nickName 昵称
     * @param int $gender 性别
     * @param int $grade 会员等级
     * @throws \think\exception\DbException
     */
    public function shopList()
    {
        $shopList = ShopModel::getAllList();
        return $this->renderSuccess($shopList,"查询成功");

    }


    /**
     * 添加店员
     * @return array|bool|mixed
     * @throws \Exception
     */
    public function add()
    {
        $param = $this->postData();
        $model = new ClerkModel;
        // 新增记录
        if ($model->add($param)) {
            return $this->renderSuccess([],'添加成功');
        }
        return $this->renderError([],$model->getError() ?: '添加失败');
    }

    /**
     * 编辑店员
     * @param $clerk_id
     * @return array|bool|mixed
     * @throws \think\exception\DbException
     */
    public function edit()
    {
        $param = $this->postData();
        if (!isset($param['clerk_id']) || isZeroNumeric($param['clerk_id'])) {
            return $this->renderError([],'参数错误');
        }
        // 店员详情
        $model = ClerkModel::detail($param['clerk_id']);
        if(!$model){
            return $this->renderError([],'店员不存在，请刷新重试');
        }

        // 编辑
        if ($model->edit($param)) {
            return $this->renderSuccess([],'更新成功');
        }
        return $this->renderError([],$model->getError() ?: '更新失败');
    }

    /**
     * 店员详情
     * @param $clerk_id
     * @return array
     * @throws \think\exception\DbException
     */
    public function detail()
    {
        $param = $this->postData();
        if (!isset($param['clerk_id']) || isZeroNumeric($param['clerk_id'])) {
            return $this->renderError([],'参数错误');
        }
        // 店员详情
        $model = ClerkModel::detail($param['clerk_id']);
        if(!$model){
            return $this->renderError([],'店员不存在，请刷新重试');
        }

        return $this->renderSuccess($model,'查询成功');
    }


    /**
     * 删除店员
     * @param $clerk_id
     * @return array
     * @throws \think\exception\DbException
     */
    public function delete()
    {
        $param = $this->postData();
        if (!isset($param['clerk_id']) || isZeroNumeric($param['clerk_id'])) {
            return $this->renderError([],'参数错误');
        }
        // 店员详情
        $model = ClerkModel::detail($param['clerk_id']);
        if(!$model){
            return $this->renderError([],'店员不存在，请刷新重试');
        }

        if (!$model->setDelete()) {
            return $this->renderError([],$model->getError() ?: '删除失败');
        }
        return $this->renderSuccess([],'删除成功');
    }

}